package com.omar.labs.tamtamy;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

import com.omar.labs.tamtamy.JSON.JSONContentFile;
import com.omar.labs.tamtamy.JSON.JSONContentLink;
import com.omar.labs.tamtamy.JSON.JSONContentList;
import com.omar.labs.tamtamy.JSON.JSONOnLineUsersList;
import com.omar.labs.tamtamy.JSON.JSONTagDetail;
import com.omar.labs.tamtamy.JSON.JSONTagList;
import com.omar.labs.tamtamy.JSON.JSONToken;
import com.omar.labs.tamtamy.JSON.JSONUserDetail;
import com.omar.labs.tamtamy.JSON.JSONUsersList;
import com.omar.labs.tamtamy.XML.XMLComment;
import com.omar.labs.tamtamy.XML.XMLCommentList;
import com.omar.labs.tamtamy.XML.XMLContentFile;
import com.omar.labs.tamtamy.XML.XMLContentLink;
import com.omar.labs.tamtamy.XML.XMLContentList;
import com.omar.labs.tamtamy.XML.XMLContentMessage;
import com.omar.labs.tamtamy.XML.XMLContentWidget;
import com.omar.labs.tamtamy.XML.XMLGroupDetails;
import com.omar.labs.tamtamy.XML.XMLGroupList;
import com.omar.labs.tamtamy.XML.XMLMetadata;
import com.omar.labs.tamtamy.XML.XMLNetworkDetails;
import com.omar.labs.tamtamy.XML.XMLNetworkList;
import com.omar.labs.tamtamy.XML.XMLNotice;
import com.omar.labs.tamtamy.XML.XMLNoticeList;
import com.omar.labs.tamtamy.XML.XMLOnLineUsersList;
import com.omar.labs.tamtamy.XML.XMLTagDetail;
import com.omar.labs.tamtamy.XML.XMLTagList;
import com.omar.labs.tamtamy.XML.XMLToken;
import com.omar.labs.tamtamy.XML.XMLUserDetail;
import com.omar.labs.tamtamy.XML.XMLUserStatsGenerationDetails;
import com.omar.labs.tamtamy.XML.XMLUserStatsGenerationList;
import com.omar.labs.tamtamy.XML.XMLUserStatsList;
import com.omar.labs.tamtamy.XML.XMLUserTag;
import com.omar.labs.tamtamy.XML.XMLUsersList;
import com.omar.labs.tamtamy.conf.JTTAProps;

/**
 * TamTamyAPI class sets up the parameters and handle the calls to the resources via URI statements.<br>
 * Uses the <b>jtta.properties</b> file to load configuration settings and the 
 * <a href="http://hc.apache.org/httpclient-3.x/" target="_new">Apache commons-httpclient</a> to manage 
 * http requests and responses from the resource.
 * 
 * @author Omar "0m4r" Adobati 
 * @version %I%, %G% 
 * 
 */

/**
 * @author Omar
 *
 */
public class  TamTamyAPI extends HttpClient{
	/**
	 * Reads configuration infos from configuration file
	 */
	protected final static JTTAProps PROP = new JTTAProps("jtta.properties");
	/**
	 * Holds the application name that uses jTTA
	 */	
	protected final static String APP_NAME = PROP.getAppName();
	/**
	 * Defines the XML request type
	 */
	public final static String XML_REQ = ".xml";	
	/**
	 * Defines the JSON request type
	 */	
	public final static String JSON_REQ = ".json";
	//protected Token token;
	/**
	 * Stores the URL to access the resource
	 */
	protected static String url;
	/**
	 * Stores the TamTamy API version
	 */
	public static String version;
	/**
	 * Stores the type of the request, XML or JSON<br>
	 * <b>this version ONLY support XML</b>
	 */
	public static String type;				
	/**
	 * counts the number of jTTA instances
	 */
	protected static long instances = 0;
	/**
	 * Turns debug mode on or off
	 */
	protected static boolean debug = false;    
	
	final public static String REQ_XML = "XML";
	final public static String REQ_JSON = "JSON";
		
	/**
	 * Class constructor.
	 * 
	 * @see HttpClient
	 * @see #url
	 * @see #version
	 * @see #type
	 * @see #debug
	 * @see #instances
	 */
	public TamTamyAPI(){ 
		super();
		TamTamyAPI.instances++;
		TamTamyAPI.url = PROP.getUrl();
		TamTamyAPI.version = PROP.getVersion();
		if(TamTamyAPI.type==null)
		  TamTamyAPI.type = PROP.getType();					
		TamTamyAPI.debug = PROP.getDebug();
	}
			
	public TamTamyAPI(String reqType){
		new TamTamyAPI();
		TamTamyAPI.type = reqType;			
	}

	/**
	 * Calls the login URI to access the resource to execute the login
	 * @see	com.omar.labs.tamtamy.Token
	 * @param usr	username to login
	 * @param pwd	password to login
	 */	
	public Token token;
	
	public void doLogin(String usr, String pwd){
		String LOGIN = TamTamyAPI.url + "/login"+TamTamyAPI.type + "?userid="+usr+"&password="+pwd+"&appName="+TamTamyAPI.APP_NAME;
		String tmp = null;				  
		tmp = doGetRequest(LOGIN);
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML)!=-1)
			token = new XMLToken(tmp);	    
		else if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_JSON)!=-1)
			token = new JSONToken(tmp);	
		
		System.out.println("token: " + token);
		System.out.println("LOGIN: " + LOGIN);
	}
	
	/**
	 * Calls the logout URI to access the resource to execute the logout
	 */
	
	public void doLogout(){
		String LOGOUT = TamTamyAPI.url + "/logout"+TamTamyAPI.type+"?token=" + token.getToken() + "&appName="+TamTamyAPI.APP_NAME;
		doGetRequest(LOGOUT);
	}

	/**
	 * Calls the URI to access the resource to have back the complete Tag List
	 * @see	com.omar.labs.tamtamy.TagList
	 * @return	com.omar.labs.tamtamy.TagList
	 */
	public TagList getTagList(long startOffset, long limit){		
		String TAG_LIST = TamTamyAPI.url + "/tag"+TamTamyAPI.type+"?token="+token.getToken();
		if(startOffset>-1){
			TAG_LIST += "&startOffset="+startOffset + "&limit="+limit;
		}
		TAG_LIST += "&appName="+TamTamyAPI.APP_NAME;
		String tags =  doGetRequest(TAG_LIST);
		TagList tl = null;
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML) != -1)
			tl = new XMLTagList(tags);
		else if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_JSON) != -1)
			tl = new JSONTagList(tags);

		return tl;		
	}

	/**
	 * Calls the URI to access the resource to have back the complete Tag Details information
	 * @see	com.omar.labs.tamtamy.TagDetail
	 * @see	com.omar.labs.tamtamy.XML.XMLTagDetail
	 * @param tag	name of the tag
	 * @return	com.omar.labs.tamtamy.TagDetail
	 */
	public TagDetail getTagDetail(String tag){
		String TAG_LIST_DETAIL = TamTamyAPI.url + "/tag/"+tag + TamTamyAPI.type + "?token=" + token.getToken() + "&appName="+TamTamyAPI.APP_NAME;
		String tagLD =  doGetRequest(TAG_LIST_DETAIL);
		TagDetail td = null;
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML) != -1)
			td = new XMLTagDetail(tagLD);
		else if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_JSON) != -1)
			td = new JSONTagDetail(tagLD);

		return td;
	}
	
	/**
	 * Calls the URI to access the resource to have back the complete Content List
	 * @see	com.omar.labs.tamtamy.XML.XMLContentList
	 * @see	com.omar.labs.tamtamy.Content
	 * @see com.omar.labs.tamtamy.ContentFilter
	 * @see com.omar.labs.tamtamy.ContentOrder
	 * @param startOffset starting point int the list
	 * @param limit number of items to display
	 * @param filter 
	 * @param order
	 * @return	com.omar.labs.tamtamy.ContentList
	 * 
	 */
	public ContentList getContentList(long startOffset, long limit, String filter, String order){
		String CONTENT = TamTamyAPI.url + "/content" + TamTamyAPI.type + "?token=" + token.getToken();		
		  CONTENT +=  "&startOffset=" + startOffset + "&limit="+limit;		
		if(filter!=null)
			CONTENT += "&filter="+order;
		if(order!=null)
			CONTENT += "&order="+filter;
		CONTENT += "&appName="+TamTamyAPI.APP_NAME;
		String contList =  doGetRequest(CONTENT);
		ContentList cl = null;
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML)  != -1)
			cl = new XMLContentList(contList);
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_JSON)  != -1)
			cl = new JSONContentList(contList);
		return cl;		
	}			
	
	/**
	 * Calls the URI to access the resource to have back the Content File information
	 * @see	com.omar.labs.tamtamy.ContentFile
	 * @see	com.omar.labs.tamtamy.XML.XMLContentFile
	 * @param	contentID the content ID
	 * @return	com.omar.labs.tamtamy.ContentFile
	 */
	public ContentFile getContentFile(String contentID){
		String CONTENT_FILE = TamTamyAPI.url + "/content/"+contentID + TamTamyAPI.type + "?token=" + token.getToken() + "&appName="+TamTamyAPI.APP_NAME;
		String contF =  doGetRequest(CONTENT_FILE);
		ContentFile cf = null;
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML) != -1)
			cf = new XMLContentFile(contF);
		else if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_JSON) != -1)
			cf = new JSONContentFile(contF);
		
		return cf;	
	}
	
	/**
	 * Calls the URI to access the resource to have back the Content Link information
	 * @see	com.omar.labs.tamtamy.ContentFile
	 * @see	com.omar.labs.tamtamy.XML.XMLContentLink
	 * @param	contentID the content ID
	 * @return	com.omar.labs.tamtamy.ContentLink
	 */
	public ContentLink getContentLink(String contentID){
		String CONTENT_FILE = TamTamyAPI.url + "/content/"+contentID + TamTamyAPI.type + "?token=" + token.getToken() + "&appName="+TamTamyAPI.APP_NAME;
		String contL =  doGetRequest(CONTENT_FILE);
		ContentLink cl = null;
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML) != -1)
			cl = new XMLContentLink(contL);
		else if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_JSON) != -1)
			cl = new JSONContentLink(contL);
		
		return cl;	
	}
	
	/**
	 * Calls the URI to access the resource to have back the Content Message information
	 * @see	com.omar.labs.tamtamy.ContentFile
	 * @see	com.omar.labs.tamtamy.XML.XMLContentMessage
	 * @param	contentID the content ID
	 * @return	com.omar.labs.tamtamy.ContentMessage
	 */
	public ContentMessage getContentMessage(String contentID){
		String CONTENT_MESSAGE = TamTamyAPI.url + "/content/"+contentID + TamTamyAPI.type + "?token=" + token.getToken() + "&appName="+TamTamyAPI.APP_NAME;
		String contL =  doGetRequest(CONTENT_MESSAGE);
		ContentMessage cl = null;
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML) != -1)
			cl = new XMLContentMessage(contL);
	
		return cl;	
	}
	
	/**
	 * Calls the URI to access the resource to have back the Content Widget information
	 * @see	com.omar.labs.tamtamy.ContentFile
	 * @see	com.omar.labs.tamtamy.XML.XMLContentWidget
	 * @param	contentID the content ID
	 * @return	com.omar.labs.tamtamy.ContentWidget
	 */
	public ContentWidget getContentWidget(String contentID){
		String CONTENT_WIDGET = TamTamyAPI.url + "/content/"+contentID + TamTamyAPI.type + "?token=" + token.getToken() + "&appName="+TamTamyAPI.APP_NAME;
		String contL =  doGetRequest(CONTENT_WIDGET);
		ContentWidget cl = null;
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML) != -1)
			cl = new XMLContentWidget(contL);

		return cl;	
	}
	
	/**
	 * Calls the URI to access the resource to have back the Comment List
	 * @see	com.omar.labs.tamtamy.Content
	 * @see	com.omar.labs.tamtamy.ContentList
	 * @see	com.omar.labs.tamtamy.XML.XMLCommentList
	 * @param startOffset starting point
	 * @param 	limit number of items to display
	 * @return	com.omar.labs.tamtamy.CommentList
	 */
	public CommentList getCommentList(long startOffset, long limit){
		String COMMENT_LIST = TamTamyAPI.url + "/comment" + TamTamyAPI.type + "?token=" + token.getToken();
		if(startOffset>-1)
		  COMMENT_LIST +=  "&startOffset=" +startOffset + "&limit="+limit;
		COMMENT_LIST += "&appName="+TamTamyAPI.APP_NAME;
		String comment_list = doGetRequest(COMMENT_LIST);
		CommentList comm = null; 
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML) != -1)
			comm = new XMLCommentList(comment_list);

		return comm;	
	}
	
	/**
	 * Calls the URI to access the resource to have back the Comment item
	 * @see	com.omar.labs.tamtamy.Content
	 * @see	com.omar.labs.tamtamy.ContentList
	 * @see	com.omar.labs.tamtamy.XML.XMLComment
	 * @param	contentID the content ID
	 * @return	com.omar.labs.tamtamy.Comment
	 */
	public Comment getComment(long commentID){
		String COMMENT = TamTamyAPI.url + "/comment/" + commentID + TamTamyAPI.type + "?token=" + token.getToken() + "&appName="+TamTamyAPI.APP_NAME;
		String comment = doGetRequest(COMMENT);
		Comment comm = null; 
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML) != -1)
			comm = new XMLComment(comment);

		return comm;	
	}
	
	/**
	 * Calls the URI to access the resource to have back the Notice List
	 * @see	com.omar.labs.tamtamy.Content
	 * @see	com.omar.labs.tamtamy.NoticeList
	 * @see	com.omar.labs.tamtamy.XML.XMLNoticeList
	 * @see com.omar.labs.tamtamy.NoticeContent
	 * @see com.omar.labs.tamtamy.NoticeContentComment
	 * @see com.omar.labs.tamtamy.NoticeBlog
	 * @see com.omar.labs.tamtamy.NoticeWiki
	 * @see com.omar.labs.tamtamy.NoticeForum
	 * @see com.omar.labs.tamtamy.NoticeUser
	 * @see com.omar.labs.tamtamy.NoticeUserTagging
	 * @param 	startOffset starting point
	 * @param 	limit number of items to display
	 * @param 	filter
	 * @return	com.omar.labs.tamtamy.CommentList
	 */
	public NoticeList getNoticeList(long startOffset, long limit, String filter){
		String NOTICE_LIST = TamTamyAPI.url + "/notice" + TamTamyAPI.type + "?token=" + token.getToken();
		if(startOffset>-1)
			NOTICE_LIST +=  "&startOffset="+startOffset + "&limit="+limit;
		if(filter!=null){
			NOTICE_LIST += "&filter="+filter;
		}
		NOTICE_LIST += "&appName="+TamTamyAPI.APP_NAME;		
		String noticeList = doGetRequest(NOTICE_LIST);
		NoticeList nl = null; 
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML) != -1)
			nl = new XMLNoticeList(noticeList);

		return nl;	
	}
	
	/**
	 * Calls the URI to access the resource to have back the Notice
	 * @see	com.omar.labs.tamtamy.Content
	 * @see	com.omar.labs.tamtamy.NoticeList
	 * @see	com.omar.labs.tamtamy.XML.XMLNoticeList
	 * @see com.omar.labs.tamtamy.NoticeContent
	 * @see com.omar.labs.tamtamy.NoticeContentComment
	 * @see com.omar.labs.tamtamy.NoticeBlog
	 * @see com.omar.labs.tamtamy.NoticeWiki
	 * @see com.omar.labs.tamtamy.NoticeForum
	 * @see com.omar.labs.tamtamy.NoticeUser
	 * @see com.omar.labs.tamtamy.NoticeUserTagging
	 * @param 	noticeID 
	 * @return	com.omar.labs.tamtamy.CommentList
	 */
	public Notice getNotice(long noticeID){
		String NOTICE = TamTamyAPI.url + "/notice/" + noticeID + TamTamyAPI.type + "?token=" + token.getToken() + "&appName="+TamTamyAPI.APP_NAME;
		String notice = doGetRequest(NOTICE);
		Notice n = null; 
		if(TamTamyAPI.type.toUpperCase().equals(TamTamyAPI.REQ_XML))
			n = new XMLNotice(notice);

		return n;	
	}
	
	public UserStatsList getUserStats(String userID, long startOffset, long limit){
		String USER_STATS = TamTamyAPI.url + "/userStats/" + userID + TamTamyAPI.type + "?token=" + token.getToken();
		if(startOffset>-1)
			USER_STATS +=  "&startOffset="+startOffset + "&limit="+limit;
		USER_STATS += "&appName="+TamTamyAPI.APP_NAME;
		
		String user_stats = doGetRequest(USER_STATS);
		UserStatsList us = null; 
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML)!=-1)
			us = new XMLUserStatsList(user_stats);

		return us;	
	}
	
	public UserStatsGenerationList getUserStatsGeneration(long startOffset, long limit){
		String USER_STATS_GENERATION_LIST = TamTamyAPI.url + "/userStatsGeneration" + TamTamyAPI.type + "?token=" + token.getToken();
		if(startOffset>-1)
			USER_STATS_GENERATION_LIST +=  "&startOffset="+startOffset + "&limit="+limit;
		USER_STATS_GENERATION_LIST += "&appName="+TamTamyAPI.APP_NAME;		
		String user_stats_genration_list = doGetRequest(USER_STATS_GENERATION_LIST);
		UserStatsGenerationList usgl = null; 
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML)!=-1)
			usgl = new XMLUserStatsGenerationList(user_stats_genration_list);
		return usgl;	
	}
	
	public UserStatsGenerationDetails getUserStatsGenerationDetails(String userStatsID, long startOffset, long limit){
		String USER_STATS_GENERATION_DETAIL = TamTamyAPI.url + "/userStatsGeneration/" + userStatsID + TamTamyAPI.type + "?token=" + token.getToken();
		if(startOffset>-1)
			USER_STATS_GENERATION_DETAIL +=  "&startOffset="+startOffset + "&limit="+limit;
		USER_STATS_GENERATION_DETAIL += "&appName="+TamTamyAPI.APP_NAME;		
		String user_stats_genration_detail = doGetRequest(USER_STATS_GENERATION_DETAIL);
		UserStatsGenerationDetails usgd = null; 
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML)!=-1)
			usgd = new XMLUserStatsGenerationDetails(user_stats_genration_detail);
		return usgd;	
	}
	
	public GroupList getGroup(long startOffset, long limit){
		String GROUP = TamTamyAPI.url + "/group" + TamTamyAPI.type + "?token=" + token.getToken();
		if(startOffset>-1)
			GROUP +=  "&startOffset="+startOffset + "&limit="+limit;		
		GROUP += "&appName="+TamTamyAPI.APP_NAME;		
		String group = doGetRequest(GROUP);
		GroupList g = null; 
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML)!=-1)
			g = new XMLGroupList(group);
		return g;	
	}
	
	public GroupDetails getGroupDetails(String groupID){
		String GROUP_DETAILS = TamTamyAPI.url + "/group/" + groupID + TamTamyAPI.type + "?token=" + token.getToken();					
		GROUP_DETAILS += "&appName="+TamTamyAPI.APP_NAME;		
		String group_details = doGetRequest(GROUP_DETAILS);
		GroupDetails gd = null; 
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML)!=-1)
			gd = new XMLGroupDetails(group_details);
		return gd;	
	}
	
	public NetworkList getNetwork(long startOffset, long limit){
		String NETWORK = TamTamyAPI.url + "/network" + TamTamyAPI.type + "?token=" + token.getToken();		
		if(startOffset>-1)
			NETWORK +=  "&startOffset="+startOffset + "&limit="+limit;		
		NETWORK += "&appName="+TamTamyAPI.APP_NAME;		
		String network = doGetRequest(NETWORK);
		NetworkList n = null; 
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML)!=-1)
			n = new XMLNetworkList(network);
		return n;	
	}
	
	public NetworkDetails getNetworkDetails(String networkID){
		String NETWORK_DETAILS = TamTamyAPI.url + "/network/" + networkID + TamTamyAPI.type + "?token=" + token.getToken();			
		NETWORK_DETAILS += "&appName="+TamTamyAPI.APP_NAME;		
		String network_details = doGetRequest(NETWORK_DETAILS);
		NetworkDetails nd = null; 
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML)!=-1)
			nd = new XMLNetworkDetails(network_details);
		return nd;	
	}
	
	public Metadata getMetadata(String url) throws Exception{
		String METADATA = TamTamyAPI.url + "/metadataGuess" + TamTamyAPI.type + "?token=" + token.getToken();
		METADATA += "&url="+ Metadata.encodeURL(url); 
		METADATA += "&appName="+TamTamyAPI.APP_NAME;		
		String metadata = doGetRequest(METADATA);
		Metadata m = null; 
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML)!=-1)
			m = new XMLMetadata(metadata);
		return m;	
	}
	
	public UserTag getUserTag(String filter) throws Exception{
		String USER_TAG = TamTamyAPI.url + "/userTag" + TamTamyAPI.type + "?token=" + token.getToken();		
		if(filter!=null){
		  USER_TAG += "&filter="+filter;
		}
		USER_TAG += "&appName="+TamTamyAPI.APP_NAME;		
		String user_tag = doGetRequest(USER_TAG);
		UserTag ut = null; 
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML)!=-1)
			ut = new XMLUserTag(user_tag);
		return ut;	
	}
	
	public UserTag getUserTag(String userID, String filter) throws Exception{
		String USER_TAG = TamTamyAPI.url + "/userTag/" + userID + TamTamyAPI.type + "?token=" + token.getToken();
		if(filter!=null){
		  USER_TAG += "&filter="+filter;
		}
		USER_TAG += "&appName="+TamTamyAPI.APP_NAME;		
		String user_tag = doGetRequest(USER_TAG);
		UserTag ut = null; 
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML)!=-1)
			ut = new XMLUserTag(user_tag);
		return ut;	
	}

	/**
	 * Calls the URI to access the resource to have back the complete User List
	 * @see	com.omar.labs.tamtamy.UsersList
	 * @see	com.omar.labs.tamtamy.XML.XMLUsersList
	 * @return	com.omar.labs.tamtamy.XML.XMLUsersList
	 */
	public UsersList getUsersList(long startOffset, long limit, String filter){
		String USER_LIST = TamTamyAPI.url + "/user"+TamTamyAPI.type+"?token=" + token.getToken();
		if(startOffset>-1)
			USER_LIST += "&startOffset="+startOffset + "&limit="+limit;
		if(filter!=null)
			USER_LIST += "&filter="+filter;
		USER_LIST += "&appName="+TamTamyAPI.APP_NAME;
		String userList =  doGetRequest(USER_LIST);
		UsersList us = null;
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML) != -1)
			us = new XMLUsersList(userList);
		else if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_JSON) != -1)
			us = new JSONUsersList(userList);
		
		return us;				
	}

	/**
	 * Calls the URI to access the resource to have back the complete On-Line User List
	 * @see	com.omar.labs.tamtamy.OnLineUsersList
	 * @see	com.omar.labs.tamtamy.XML.XMLOnLineUsersList
	 * @return	com.omar.labs.tamtamy.XML.XMLOnLineUsersList
	 */ 	 
	public OnLineUsersList getOnLineUsersList(long startOffset, long limit){
		String USER_LIST = TamTamyAPI.url + "/onLineUser"+TamTamyAPI.type+"?token=" + token.getToken();
		if(startOffset>-1){
			USER_LIST += "&startOffset="+startOffset+"&limit="+limit; 
		}
		USER_LIST += "&appName="+TamTamyAPI.APP_NAME;
		String userList =  doGetRequest(USER_LIST);				
		OnLineUsersList us = null;
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML) != -1)
			us = new XMLOnLineUsersList(userList);
		else if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_JSON) != -1)
			us = new JSONOnLineUsersList(userList);
		
		return us;	
	}

	/**
	 * Calls the URI to access the resource to have back the User Detail
	 * @see	com.omar.labs.tamtamy.UserDetail
	 * @see	com.omar.labs.tamtamy.XML.XMLUserDetail
	 * @param userID
	 * @return	com.omar.labs.tamtamy.XML.XMLUserDetail	 
	 */
	public UserDetail getUserDetail(String userID){
		String USER = TamTamyAPI.url + "/user/"+userID+TamTamyAPI.type+"?token="+token.getToken() + "&appName="+TamTamyAPI.APP_NAME;
		String user =  doGetRequest(USER);
		//return new XMLUserDetail(user);	
		UserDetail ud = null;
		if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_XML) != -1)
			ud = new XMLUserDetail(user);
		else if(TamTamyAPI.type.toUpperCase().indexOf(TamTamyAPI.REQ_JSON) != -1)
			ud = new JSONUserDetail(user);
		
		return ud;	
		
	}
	
	public void downloadContent(String contentID){
		String CONTENT_DOWNLOAD = TamTamyAPI.url + "/contentDownload/"+contentID + "?token=" + token.getToken() + "&appName="+TamTamyAPI.APP_NAME;
		doGetRequest(CONTENT_DOWNLOAD);		
	}
	

	/**
	 * Returns the response to URI calls to a resource. 
	 * 
	 * @see org.apache.commons.httpclient.methods#GetMethod(String)
	 * 
	 * @param url	URI to access the resource
	 * @return String	response from the resource
	 */
	protected String doGetRequest(String url){
		GetMethod getReq = new GetMethod(url);
		String resp = null;
		try {
			this.executeMethod(getReq);
			if(TamTamyAPI.debug){
				System.out.println("doGetRequest: " + url + "\n" +getReq.getStatusCode() + " - " + getReq.getStatusText() + "\n");				
			}
			resp = getReq.getResponseBodyAsString().trim();
			if(TamTamyAPI.debug){
				System.out.println("response: ");
				System.out.println(resp);
			}
		} catch (Exception e) {
			System.out.println(e.getLocalizedMessage());
			if(TamTamyAPI.debug){
				e.printStackTrace();
			}
		}				
		getReq.releaseConnection();		
		return resp;
	}		
	
	public String getUsername(){
		return PROP.getUsername();
	}
	
	public String getPassword(){
		return PROP.getPassword();
	}
}
